package cn.itcast.job;

import com.sun.jersey.api.client.Client;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.jar.Manifest;


@Component
public class CleanImgJob {

    @Autowired
    JedisPool jedisPool;



    public void cleanImg() {

        //大集合减去小集合 获得出垃圾图片的信息
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,
                RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        //图片服务器路径
        String path = "http://localhost:8080/fileServer/upload/";

        System.out.println("任务启动了");

        //健壮
        if(set != null && set.size() > 0) {

            //创建客户端
            Client client = Client.create();

            for(String picName : set) {

                //删除redis服务器中的垃圾图片
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);

                //连接服务器，完成跨服器的图片删除
                client.resource(path+picName).delete();
            }
        }
    }



    public static void main(String[] args) throws Exception {

        XSSFWorkbook book = new XSSFWorkbook("xxxxx");

        XSSFSheet sheetAt = book.getSheetAt(0);

        for(Row row : sheetAt) {

            for(Cell cell : row) {

                cell.setCellValue(11);
            }
        }

        FileOutputStream fileOutputStream = FileUtils.openOutputStream(new File("c:\\a.xlsx"));

        book.write(fileOutputStream);
        fileOutputStream.flush();
        book.close();


    }
}
